package com.lingjtx.common.security.limit;

import com.lingjtx.common.core.RedisOptions;

import java.util.ArrayList;
import java.util.List;

/**
 * 状态委托环境
 */
public class ApiContext<Point, Request> {

    private final List<HandlerState<Point, Request>> handlers = new ArrayList<>();

    public void setHandlers(HandlerState<Point, Request> handlerState) {
        handlerState.setContext(this);
        handlers.add(handlerState);
    }

    public Object process(Point point, Request request, RedisOptions redis) {
        for (HandlerState<Point, Request> handler : handlers) {
            handler.handler(point, request, redis);
            if (handler.errorState) {
                return handler.retError();
            }
        }
        return point;
    }
}
